﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using BookStore.Data;

namespace BookStore.Bussiness
{
   public class Cart
    {
        public ArrayList arrCart;
        public Cart()
        {
            arrCart = new ArrayList();
        }
     
       // public System.Collections.Generic.List<productInfo> pro;       
       ////them san pham vao gio hang
       // public void AddCart(productInfo probook)
       // {
       //     pro.Add(probook);
       // }

        public void AddToCart( productInfo sp)
        {
            int index = getIndexOfSpInCart(sp);
            if (index == -1)
                //them moi
                arrCart.Add(sp);
            else
                ((productInfo)arrCart[index]).Number++;

        }
        public int getIndexOfSpInCart(productInfo sp)
        {
            int j = -1;
            for (int i = 0; i < arrCart.Count; i++)
            {
                productInfo objSP = (productInfo)arrCart[i];
                if (objSP.productID == sp.productID)
                {
                    j = i;
                    break;
                }
            }
            return j;
        }
        public void Update(productInfo sp) 
        {
            int index = getIndexOfSpInCart(sp);
            if (index == 1)
                //them moi
                arrCart.Add(sp);
            else
                ((productInfo)arrCart[index]).Number++;
        }
        public void RemoveCart(productInfo sp)
        {
            for (int i = 0; i < arrCart.Count; i++)
            {
                productInfo objSP = (productInfo)arrCart[i];
                if (objSP.productID == sp.productID)
                {
                    arrCart.Remove(objSP);
                    break;
                }
            }
        }
        public void RemoveAllCart()
        {
            arrCart.Clear();
        }
    }
}
